package com.xnfh.image

import android.graphics.Bitmap
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.util.Preconditions
import com.bumptech.glide.util.Util
import java.nio.ByteBuffer
import java.security.MessageDigest

/**
 * @Description:    设置不同圆角
 * @Author: ziheng
 * @CreateDate: 2022/9/21 4:51 下午
 * @UpdateUser: ziheng
 * @UpdateDate: 2022/9/21 4:51 下午
 * @UpdateRemark: 更新说明
 */
class RoundBitmapTransformation(
    leftTopRadius: Int, rightTopRadius: Int,
    leftBottomRadius: Int, rightBottomRadius: Int
) : BitmapTransformation() {
    private val leftTopRadius: Int
    private val rightTopRadius: Int
    private val leftBottomRadius: Int
    private val rightBottomRadius: Int

    /**
     * @param leftTopRadius     the corner radius of Left (in device-specific pixels).
     * @param rightTopRadius    the corner radius of Top (in device-specific pixels).
     * @param leftBottomRadius  the corner radius of Right (in device-specific pixels).
     * @param rightBottomRadius the corner radius of Bottom (in device-specific pixels).
     * @throws IllegalArgumentException if rounding radius is 0 or less.
     */
    init {
        Preconditions.checkArgument(leftTopRadius >= 0, "leftTopRadius must be greater than 0.")
        Preconditions.checkArgument(rightTopRadius >= 0, "rightTopRadius must be greater than 0.")
        Preconditions.checkArgument(
            leftBottomRadius >= 0,
            "leftBottomRadius must be greater than 0."
        )
        Preconditions.checkArgument(
            rightBottomRadius >= 0,
            "rightBottomRadius must be greater than 0."
        )
        this.leftTopRadius = leftTopRadius
        this.rightTopRadius = rightTopRadius
        this.leftBottomRadius = leftBottomRadius
        this.rightBottomRadius = rightBottomRadius
    }

    override fun transform(
        pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int
    ): Bitmap {
        return RoundTransformationUtils.roundedCorners(
            pool, toTransform,
            leftTopRadius, rightTopRadius, leftBottomRadius, rightBottomRadius
        )
    }

    override fun equals(o: Any?): Boolean {
        if (o is RoundBitmapTransformation) {
            val other = o
            return leftTopRadius == other.leftTopRadius && rightTopRadius == other.rightTopRadius && leftBottomRadius == other.leftBottomRadius && rightBottomRadius == other.rightBottomRadius
        }
        return false
    }

    override fun hashCode(): Int {
        return Util.hashCode(
            ID.hashCode(),
            Util.hashCode(
                leftTopRadius,
                Util.hashCode(
                    rightTopRadius,
                    Util.hashCode(
                        leftBottomRadius,
                        Util.hashCode(rightBottomRadius)
                    )
                )
            )
        )
    }

    override fun updateDiskCacheKey(messageDigest: MessageDigest) {
        messageDigest.update(ID_BYTES)
        val leftRadiusData = ByteBuffer.allocate(4).putInt(leftTopRadius).array()
        messageDigest.update(leftRadiusData)
        val topRadiusData = ByteBuffer.allocate(4).putInt(rightTopRadius).array()
        messageDigest.update(topRadiusData)
        val rightRadiusData = ByteBuffer.allocate(4).putInt(leftBottomRadius).array()
        messageDigest.update(rightRadiusData)
        val bottomRadiusData = ByteBuffer.allocate(4).putInt(rightBottomRadius).array()
        messageDigest.update(bottomRadiusData)
    }

    companion object {
        private const val ID = "com.bumptech.glide.load.resource.bitmap.RoundedCorners"
        private val ID_BYTES = ID.toByteArray(CHARSET)
    }
}